﻿using System;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Linq;
using Nydus.Model.Entities;
using Nydus.Model.Interfaces;

namespace Nydus.Model
{
    public partial class NydusEntities : ObjectContext, INydusDataContext
    {
        public const string ConnectionString = "name=NydusEntities";
        public const string ContainerName = "NydusEntities";

        #region Constructors

        public NydusEntities()
            : base(ConnectionString, ContainerName)
        {
            this.ContextOptions.LazyLoadingEnabled = false;
        }

        public NydusEntities(string connectionString)
            : base(connectionString, ContainerName)
        {
            this.ContextOptions.LazyLoadingEnabled = false;
        }

        public NydusEntities(EntityConnection connection)
            : base(connection, ContainerName)
        {
            this.ContextOptions.LazyLoadingEnabled = false;
        }

        #endregion

        #region ObjectSet Properties

        public IObjectSet<AgentGroup> AgentGroups
        {
            get { return _agentGroups ?? (_agentGroups = CreateObjectSet<AgentGroup>("AgentGroups")); }
        }
        private IObjectSet<AgentGroup> _agentGroups;

        public IObjectSet<Agent> Agents
        {
            get { return _agents ?? (_agents = CreateObjectSet<Agent>("Agents")); }
        }
        private IObjectSet<Agent> _agents;

        public IObjectSet<Agents_AgentGroups> Agents_AgentGroups
        {
            get { return _agents_AgentGroups ?? (_agents_AgentGroups = CreateObjectSet<Agents_AgentGroups>("Agents_AgentGroups")); }
        }
        private IObjectSet<Agents_AgentGroups> _agents_AgentGroups;

        public IObjectSet<Call> Calls
        {
            get { return _calls ?? (_calls = CreateObjectSet<Call>("Calls")); }
        }
        private IObjectSet<Call> _calls;

        public IObjectSet<Customer> Customers
        {
            get { return _customers ?? (_customers = CreateObjectSet<Customer>("Customers")); }
        }
        private IObjectSet<Customer> _customers;

        public IObjectSet<Request> Requests
        {
            get { return _requests ?? (_requests = CreateObjectSet<Request>("Requests")); }
        }
        private IObjectSet<Request> _requests;

        public IObjectSet<Task> Tasks
        {
            get { return _tasks ?? (_tasks = CreateObjectSet<Task>("Tasks")); }
        }
        private IObjectSet<Task> _tasks;

        #endregion


        public string Save()
        {
            DetectChanges();
            var deletedEntities = ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Deleted)
                                                    .Select(entry => entry.Entity)
                                                    .OfType<IActivable>()
                                                    .ToList();
            foreach (var entity in deletedEntities)
            {
                ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);

                IActivable activable = (IActivable)entity;
                activable.IsActive = false;
                activable.DateTo = DateTime.Now;
            }

            // Validation part omitted
            SaveChanges();

            return String.Empty;
        }
        
        /// <summary>
        /// Gets the managed entities collection of a specified type.
        /// </summary>
        /// <typeparam name="T">Managed entity type</typeparam>
        /// <returns>Managed entity collection.</returns>
        public System.Collections.Generic.IEnumerable<T> GetManagedEntities<T>()
        {
            var oses 
                = ObjectStateManager.GetObjectStateEntries<T>();
            return oses.Select(t => (T)t.Entity );
        }
    }
}
